package cn.ultrarisk.scadmin.manager.biz.monitor.dao;

import cn.ultrarisk.mybatis.extension.domain.Page;
import cn.ultrarisk.mybatis.extension.query.Query;
import cn.ultrarisk.scadmin.manager.biz.monitor.entity.SlowLog;
import cn.ultrarisk.scadmin.manager.support.BaseDAO;
import org.springframework.stereotype.Repository;

/**
 * 慢查询日志数据操作类
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/3/28
 * @since V1.0
 */
@Repository
public class SlowLogDAO extends BaseDAO<SlowLog, Long> {
    /**
     * 构造器
     */
    public SlowLogDAO() {
        super(SlowLog.class, SlowLog.COLUMN_PROPERTY_MAP);
    }

    public Page<SlowLog> findPage(Integer collectionId, Long elapsedTime, Long optStartTime, Long optEndTime,
                                   Integer elapsedTimeMin, int pageNo, int pageSize) {
        Query query = Query.query(SlowLog.COLLECTION_ID, collectionId);
        query.gte(SlowLog.ELAPSED_TIME, elapsedTime);
        if (optStartTime != null && optStartTime > 0) {
            query.gt(SlowLog.START_TIME, optStartTime);
        }
        if (optEndTime != null && optEndTime > 0) {
            query.lt(SlowLog.START_TIME, optEndTime);
        }
        if (elapsedTimeMin != null) {
            query.gte(SlowLog.ELAPSED_TIME, elapsedTimeMin);
        }
        query.addSort(SlowLog.START_TIME, Query.Direction.DESC);
        return findPage(query, pageNo, pageSize, SlowLog.COLLECTION_ID, SlowLog.TARGET, SlowLog.IP, SlowLog.START_TIME, SlowLog.ELAPSED_TIME);
    }
}
